package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.EquipmentManufacturer;
import com.mxpio.erp.common.equipment.service.EquipmentManufacturerService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;


@Tag(name = "EquipmentManufacturerController}", description = "设备制造商")
@RestController
@RequestMapping("/erp/equipment/manufacturer/")
public class EquipmentManufacturerController {

    @Autowired
    private EquipmentManufacturerService service;


    @GetMapping("list")
    @Operation(summary = "设备制造商列表", description = "获取设备制造商列表（无分页）", method = "GET")
    public Result<List<EquipmentManufacturer>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentManufacturer> list = service.list(EquipmentManufacturer.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "设备制造商列表", description = "获取设备制造商列表（分页）", method = "GET")
    public Result<Page<EquipmentManufacturer>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EquipmentManufacturer> pageResult = service.listPage(EquipmentManufacturer.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{pnCode}")
    @Operation(summary = "根据编码获取设备制造商列表", description = "根据编码获取设备制造商列表", method = "GET")
    public Result<EquipmentManufacturer> getById(@PathVariable(name = "pnCode", required = true) String pnCode) {
        EquipmentManufacturer manufacturer = service.getById(EquipmentManufacturer.class, pnCode);
        return Result.OK(manufacturer);
    }

    @RequestMapping("add")
    @Operation(summary = "保存设备制造商", description = "保存设备制造商")
    public Result<EquipmentManufacturer> add(@RequestBody EquipmentManufacturer manufacturer) {
        service.save(manufacturer);
        return Result.OK(manufacturer);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑设备制造商", description = "编辑设备制造商", method = "PUT")
    public Result<EquipmentManufacturer> edit(@RequestBody EquipmentManufacturer manufacturer) {
        service.update(manufacturer);
        return Result.OK(manufacturer);
    }

    @DeleteMapping("remove/{pnCodes}")
    @Operation(summary = "根据编码删除设备制造商", description = "删除设备制造商,多个编码用英文逗号分隔", method = "DELETE")
    public Result<?> remove(@PathVariable(name = "pnCodes", required = true) String pnCodes) {
        String[] id = pnCodes.split(",");
        for(String key : id){
            service.delete(EquipmentManufacturer.class, key);
        }
        return Result.OK();
    }

}
